﻿using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;

namespace CustomUIConfig.SwaggerFilters
{

    public class AutoHttpMethodOperationFitler : IDocumentFilter
    {
        private readonly string _defaultHttpMethod;
        public AutoHttpMethodOperationFitler(string defaultHttpMethod = null)
        {
            _defaultHttpMethod = defaultHttpMethod;
        }

        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            //注意,不能置为null
            swaggerDoc.Components = new OpenApiComponents() { Schemas = new Dictionary<string, OpenApiSchema>() };

            OpenApiPaths paths =swaggerDoc.Paths;
            IDictionary<string, IOpenApiExtension> Extensions = paths.Extensions;

            foreach (KeyValuePair<string,OpenApiPathItem> path in paths)
            {
                int d = 0;

                OpenApiPathItem apiPath = path.Value;

                foreach (KeyValuePair<OperationType, OpenApiOperation> operation in apiPath.Operations)
                {
                    int d1 = 0;

                    OpenApiOperation apiOperation = operation.Value;

                    OpenApiResponses Responses = new OpenApiResponses();

                    
                    var keyValues= apiOperation.Responses.Where(res => res.Key == "200");
                    foreach (var keyValue in keyValues)
                    {
                        OpenApiResponse openApiResponse = keyValue.Value;

                        var Content  = new Dictionary<string, OpenApiMediaType>();

                       var jsonContent= openApiResponse.Content.FirstOrDefault(c=>c.Key.ToLower()== "application/json");
                        Content.Add(jsonContent.Key, jsonContent.Value);
                        //todo:测试
                        Content.Add("heihei", jsonContent.Value);
                        openApiResponse.Content = Content;

                        Responses.Add(keyValue.Key, openApiResponse);
                    }

                    //todo:测试
                    Responses.Add("2000", new OpenApiResponse() { Description="hahaha"});

                    //仅保留200的响应码
                    apiOperation.Responses = Responses;

                    //foreach (KeyValuePair<string, OpenApiResponse> response in apiOperation.Responses)
                    //{
                    //    int d12 = 0;

                    //    OpenApiResponse apiResponse = response.Value;

                    //    //foreach (KeyValuePair<string, OpenApiResponse> res  in apiResponse)
                    //    //{
                    //    //    int d123 = 0;

                    //    //    OpenApiResponse apiResponse = response.Value;


                    //    //}
                    //}

                }



            }
            //通过DocumentFilterContext上下文可以获取到ApiDescription集合
            foreach (var apiDescription in context.ApiDescriptions)
            {
                //为null说明没有给Action添加HttpMethod
                if (string.IsNullOrEmpty(apiDescription.HttpMethod))
                {
                    //这些逻辑是和AutoHttpMethodIfActionNoBind扩展方法保持一致的
                    var actionName = apiDescription.ActionDescriptor.RouteValues["action"];
                    string methodName = "POST";
                    if (actionName.StartsWith("get", StringComparison.OrdinalIgnoreCase))
                    {
                        methodName = "GET";
                    }
                    else if (actionName.StartsWith("put", StringComparison.OrdinalIgnoreCase))
                    {
                        methodName = "PUT";
                    }
                    else if (actionName.StartsWith("delete", StringComparison.OrdinalIgnoreCase))
                    {
                        methodName = "DELETE";
                    }
                    apiDescription.HttpMethod = methodName;
                }
            }
        }

    }
}
